package com.LA_houseLease.admin.service.impl;


import com.LA_houseLease.admin.mapper.*;
import com.LA_houseLease.admin.service.*;
import com.LA_houseLease.admin.vo.attr.AttrValueVo;
import com.LA_houseLease.admin.vo.graph.GraphVo;
import com.LA_houseLease.admin.vo.room.RoomDetailVo;
import com.LA_houseLease.admin.vo.room.RoomItemVo;
import com.LA_houseLease.admin.vo.room.RoomQueryVo;
import com.LA_houseLease.admin.vo.room.RoomSubmitVo;
import com.LA_houseLease.common.constant.RedisConstant;
import com.LA_houseLease.pojo.entity.*;
import com.LA_houseLease.pojo.enums.ItemType;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 保存或更新房间信息
     *
     * @param roomSubmitVo
     */
    @Override
    public void saveOrUpdateRoomInfo(RoomSubmitVo roomSubmitVo) {
        //判断是新增还是更新
        boolean isUpdate = roomSubmitVo.getId() != null;
        //先新增或更新房间信息表
        super.saveOrUpdate(roomSubmitVo);
        if (isUpdate) {
            //更新
            //删除房间图片列表
            LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            lambdaQueryWrapper.eq(GraphInfo::getItemId, roomSubmitVo.getId());
            graphInfoService.remove(lambdaQueryWrapper);
            //删除房间属性信息列表
            LambdaQueryWrapper<RoomAttrValue> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
            roomAttrValueService.remove(lambdaQueryWrapper1);
            //删除房间配套信息列表
            LambdaQueryWrapper<RoomFacility> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper2.eq(RoomFacility::getRoomId, roomSubmitVo.getId());
            roomFacilityService.remove(lambdaQueryWrapper2);
            //删除房间标签信息列表
            LambdaQueryWrapper<RoomLabel> lambdaQueryWrapper3 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper3.eq(RoomLabel::getRoomId, roomSubmitVo.getId());
            roomLabelService.remove(lambdaQueryWrapper3);
            //删除房间支付方式列表
            LambdaQueryWrapper<RoomPaymentType> lambdaQueryWrapper4 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper4.eq(RoomPaymentType::getRoomId, roomSubmitVo.getId());
            roomPaymentTypeService.remove(lambdaQueryWrapper4);
            //删除房间可选租期列表
            LambdaQueryWrapper<RoomLeaseTerm> lambdaQueryWrapper5 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper5.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
            roomLeaseTermService.remove(lambdaQueryWrapper5);
            //删除缓存
            String key= RedisConstant.APP_ROOM_PREFIX+roomSubmitVo.getId();
            redisTemplate.delete(key);
        }
        //插入房间图片列表
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        ArrayList<GraphInfo> graphInfos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                BeanUtils.copyProperties(graphVo, graphInfo);
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }
        //插入房间属性信息列表
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        ArrayList<RoomAttrValue> roomAttrValues = new ArrayList<>();
        if (!CollectionUtils.isEmpty(attrValueIds)) {
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().build();
                roomAttrValue.setRoomId(roomSubmitVo.getId());
                roomAttrValue.setAttrValueId(attrValueId);
                roomAttrValues.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValues);
        }
        //插入房间配套信息列表
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        ArrayList<RoomFacility> roomFacilities = new ArrayList<>();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = RoomFacility.builder().build();
                roomFacility.setFacilityId(facilityInfoId);
                roomFacility.setRoomId(roomSubmitVo.getId());
                roomFacilities.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilities);
        }
        //插入房间标签信息列表
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        ArrayList<RoomLabel> roomLabels = new ArrayList<>();
        if (!CollectionUtils.isEmpty(labelInfoIds)) {
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder().build();
                roomLabel.setLabelId(labelInfoId);
                roomLabel.setRoomId(roomSubmitVo.getId());
                roomLabels.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabels);
        }
        //插入房间支付方式列表
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        ArrayList<RoomPaymentType> roomPaymentTypes = new ArrayList<>();
        if (!CollectionUtils.isEmpty(paymentTypeIds)) {
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().build();
                roomPaymentType.setPaymentTypeId(paymentTypeId);
                roomPaymentType.setRoomId(roomSubmitVo.getId());
                roomPaymentTypes.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypes);
        }
        //插入房间可选租期列表
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
        if (!CollectionUtils.isEmpty(leaseTermIds)) {
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().build();
                roomLeaseTerm.setLeaseTermId(leaseTermId);
                roomLeaseTerm.setRoomId(roomSubmitVo.getId());
                roomLeaseTerms.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }
    }

    /**
     * 根据条件分页查询房间列表
     *
     * @param page
     * @param queryVo
     * @return
     */
    @Override
    public IPage<RoomItemVo> pageItem(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(page, queryVo);
    }

    /**
     * 根据id获取房间详细信息
     *
     * @param id
     * @return
     */
    @Override
    public RoomDetailVo getDetailById(Long id) {
        //获取房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        //获取所属公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        //获取图片列表
        List<GraphVo> graphVoList = graphInfoMapper.selectGraphVoList(ItemType.ROOM, id);
        //获取属性信息列表
        List<AttrValueVo> attrValueVoList = attrValueMapper.attrValueVoList(id);
        //获取配套信息列表
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.facilityInfoList(id);
        //获取标签信息列表
        List<LabelInfo> labelInfoList = labelInfoMapper.labelInfoList(id);
        //获取支付方式列表
        List<PaymentType> paymentTypeList = paymentTypeMapper.paymentTypeList(id);
        //获取可选租期列表
        List<LeaseTerm> leaseTermList = leaseTermMapper.leaseTermList(id);
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        return roomDetailVo;
    }


    /**
     * 根据id删除房间信息
     *
     * @param id
     */
    @Override
    public void removeRoomById(Long id) {
        //删除房间信息
        roomInfoMapper.deleteById(id);
        //删除房间图片列表
        LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        lambdaQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoService.remove(lambdaQueryWrapper);
        //删除房间属性信息列表
        LambdaQueryWrapper<RoomAttrValue> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueService.remove(lambdaQueryWrapper1);
        //删除房间配套信息列表
        LambdaQueryWrapper<RoomFacility> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.eq(RoomFacility::getRoomId, id);
        roomFacilityService.remove(lambdaQueryWrapper2);
        //删除房间标签信息列表
        LambdaQueryWrapper<RoomLabel> lambdaQueryWrapper3 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper3.eq(RoomLabel::getRoomId, id);
        roomLabelService.remove(lambdaQueryWrapper3);
        //删除房间支付方式列表
        LambdaQueryWrapper<RoomPaymentType> lambdaQueryWrapper4 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper4.eq(RoomPaymentType::getRoomId, id);
        roomPaymentTypeService.remove(lambdaQueryWrapper4);
        //删除房间可选租期列表
        LambdaQueryWrapper<RoomLeaseTerm> lambdaQueryWrapper5 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper5.eq(RoomLeaseTerm::getRoomId, id);
        roomLeaseTermService.remove(lambdaQueryWrapper5);
        //删除缓存
        String key= RedisConstant.APP_ROOM_PREFIX+id;
        redisTemplate.delete(key);
    }


    /**
     * 根据公寓id查询房间列表
     *
     * @param id
     * @return
     */
    @Override
    public List<RoomInfo> listBasicByApartmentId(Long id) {
        return roomInfoMapper.listBasicByApartmentId(id);
    }

}














